﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
    /// <summary>
    /// 提交表单数据.
    /// </summary>
    class FormDataSubmitWorker
    {
        //CID 0859 //2012.03.20 //throwsException
        //CID 1143 //2012.03.21 //calledByCodes
        public static void submit(Form form, bool throwsException, bool calledByCodes)
        {
            //CID 0859 //2012.03.19 //Begin
            int errorCount = FormIsValidateWorker.Validate(form);
            if (errorCount > 0)
            {
                if (form.ModalMessageList == null)
                {
                    form.ModalMessageList = new ArrayList();
                }
                if (throwsException)
                {
                    throw new InfoJetException(MessageUtil.FormCannotBeSubmittedForValidationErrors(), null);
                }
                else
                {
                    form.ModalMessageList.Add(MessageUtil.FormCannotBeSubmittedForValidationErrors());
                }
            }
            //CID 0859 //2012.03.19 //End
            Submit submit = form.Manifest.Submit;
            //CID 0474 //2009.12.23 //Begin
            form.Logger.LogUpdate(null);
            //CID 0474 //2009.12.23 //End
            string submitName = "";
            bool submitSuccess = false;
            Exception submitException = null;
            try
            {
                //CID 0263 //2009.03.31 //Begin
                if (submit.RuleSet != null)
                {
                    submitName = "with Rules";
                    string buttonId = (string)form.ParamTable[JetConstant.XDOC_PARAM_BUTTON];
                    string buttonSource = (string)form.ParamTable[JetConstant.XDOC_PARAM_BUTTON_SOURCE];
                    FormRuleButtonWorker.runRule(form, buttonId, buttonSource, submit.RuleSet);
                    submitSuccess = true;
                }
                //CID 0263 //2009.03.31 //End
                if (submit.UseScriptHandler)
                {
                    submitName = "via Script";
                    submitSuccess = FormCodeDispatchWorker.OnSubmitRequest(form);
                }
                if (submit.HttpAdapter != null)
                {
                    submitName = "to Web Server";
                    SubmitToWebServerWorker.submit2WebServer(form, submit.HttpAdapter);
                    submitSuccess = true;
                }
                if (submit.WSAdapter != null)
                {
                    submitName = submit.WSAdapter.ServiceUrl;
                    DataWSAdapter wsAdapter = (DataWSAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.WSAdapter);
                    DataObjectWSSubmitWorker.submitDoc(form, wsAdapter);
                    submitSuccess = true;
                }
                if (submit.DavAdapter != null)
                {
                    submitName = submit.DavAdapter.Name;
                    DataDavAdapter davAdapter = (DataDavAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.DavAdapter);
                    DataObjectDavSubmitWorker.submitDoc(form, davAdapter);
                    submitSuccess = true;
                }
                if (submit.EmailAdapter != null)
                {
                    submitName = submit.EmailAdapter.Name;
                    DataEmailAdapter emailAdapter = (DataEmailAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.EmailAdapter);
                    DataObjectEmailSubmitWorker.submitDoc(form, emailAdapter);
                    submitSuccess = true;
                }
                if (submit.AdoAdapter != null)
                {
                    submitName = submit.AdoAdapter.Name;
                    DataADOAdapter adoAdapter = (DataADOAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.AdoAdapter);
                    DataObjectDBSubmitWorker.submitDoc(form, adoAdapter);
                    submitSuccess = true;
                }
                //CID 1109 //2012.02.23 //Begin
                if (submit.SPListAdapter != null)
                {
                    submitName = submit.SPListAdapter.Name;
                    DataSPListAdapter spListAdapter = (DataSPListAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.SPListAdapter);
                    DataObjectSPSubmitWorker.submitDoc(form, spListAdapter);
                    submitSuccess = true;
                }
                //CID 1109 //2012.02.23 //End
                //CID 1134 //2012.03.15 //Begin
                if (submit.SubmitToHostAdapter != null)
                {
                    submitName = submit.SubmitToHostAdapter.Name;
                    SubmitToHostAdapter submitToHostAdapter = (SubmitToHostAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.SubmitToHostAdapter);
                    SubmitToHostWorker.submitDoc(form, submitToHostAdapter);
                    submitSuccess = true;
                    //CID 0859 //2012.03.20 //Comment
                    //如果是SubmitToHostAdapter，不显示提示消息，不支持提交后关闭窗口。
                    //return;
                    //CID 0859 //2012.03.20 //End
                }
                //CID 1134 //2012.03.15 //End
            }
            catch (Exception e)
            {
                //CID 0722 //2010.12.05 //Begin
                if (e is InfoJetException)
                {
                    InfoJetException jete = e as InfoJetException;
                    if (jete.ErrorCode != null && jete.ErrorCode.Equals("FormCode"))
                    {
                        throw e;
                    }
                    //CID 1137 //2012.03.17 //Begin
                    else
                    {
                        submitException = e;
                    }
                    //CID 1137 //2012.03.17 //End
                }
                //CID 1137 //2012.03.17 //Begin
                else
                {
                    bool triedAgain = false;
                    try
                    {
                        if (submit.DavAdapter != null)
                        {
                            UnauthorizedConnection unauthConnection = DataWebUnauthorizedWorker.checkUnauthorizedWebAdapter(form, submit.DavAdapter, e);
                            if (unauthConnection != null)
                            {
                                UnauthorizedConnection loginConnection = DataWebUnauthorizedWorker.findMatchedUnauthorizedConnection(form, unauthConnection);
                                if (loginConnection != null)
                                {
                                    triedAgain = true;
                                    DataDavAdapter davAdapter = (DataDavAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.DavAdapter);
                                    DataObjectDavSubmitWorker.submitDoc(form, davAdapter, loginConnection);
                                    form.UnauthorizedConnectionList.Remove(unauthConnection);
                                    submitSuccess = true;
                                }
                            }
                        }
                        if (submit.WSAdapter != null)
                        {
                            UnauthorizedConnection unauthConnection = DataWebUnauthorizedWorker.checkUnauthorizedWebAdapter(form, submit.WSAdapter, e);
                            if (unauthConnection != null)
                            {
                                UnauthorizedConnection loginConnection = DataWebUnauthorizedWorker.findMatchedUnauthorizedConnection(form, unauthConnection);
                                if (loginConnection != null)
                                {
                                    triedAgain = true;
                                    DataWSAdapter wsAdapter = (DataWSAdapter)DataAdapterTableUtil.getAvaliableAdapter(form, submit.WSAdapter);
                                    DataObjectWSSubmitWorker.submitDoc(form, wsAdapter, loginConnection);
                                    form.UnauthorizedConnectionList.Remove(unauthConnection);
                                    submitSuccess = true;
                                }
                            }
                        }
                        if (submit.HttpAdapter != null)
                        {
                            UnauthorizedConnection unauthConnection = DataWebUnauthorizedWorker.checkUnauthorizedWebAdapter(form, submit.HttpAdapter, e);
                            if (unauthConnection != null)
                            {
                                UnauthorizedConnection loginConnection = DataWebUnauthorizedWorker.findMatchedUnauthorizedConnection(form, unauthConnection);
                                if (loginConnection != null)
                                {
                                    triedAgain = true;
                                    SubmitToWebServerWorker.submit2WebServer(form, submit.HttpAdapter, loginConnection);
                                    form.UnauthorizedConnectionList.Remove(unauthConnection);
                                    submitSuccess = true;
                                }
                            }
                        }
                    }
                    catch (Exception exc)
                    {
                        submitException = exc;
                    }

                    if (!triedAgain)
                    {
                        submitException = e;
                    }
                }
                //CID 0722 //2010.12.05 //End
                //如果遇到异常,并且异常是由Unauthorized引发,再试一次.
                //CID 1137 //2012.03.17 //End
            }

            //CID 0939 //2011.06.02 //Begin
            if (submitSuccess)
            {
                if (submit.ShowStatusDialog)
                {
                    //CID 1185 //2012.04.30 //Begin
                    if (submit.SuccessMessage != null)
                    {
                        form.ModalMessageList.Add(submit.SuccessMessage);
                    }
                    else
                    {
                        form.ModalMessageList.Add(MessageUtil.FormSubmitted());
                    }
                    //CID 1185 //2012.04.30 //End
                }

                //CID 1143 //2012.03.21 //if
                if (!calledByCodes)
                {
                    if (submit.OnAfterSubmit.Equals(Submit.AfterSubmit_close))
                    {
                        form.IsCloseDoc = true;
                        form.IsPromptToSaveChanges = false;
                    }
                }
                //CID 1143 //2012.03.21 //End
            }
            else
            {
                //CID 1134 //2012.03.14 //Begin
                FileLog.log(submitException);
                //CID 1134 //2012.03.14 //End
                if (submit.ErrorMessage != null)
                {
                    //CID 1137 //2012.03.17 //Begin
                    //CID 0859 //2012.03.20 //Begin
                    if (throwsException)
                    {
                        throw new InfoJetException(submit.ErrorMessage + "\r\n" + submitException.Message, null);
                    }
                    else
                    {
                        form.ModalMessageList.Add(submit.ErrorMessage + "\r\n" + submitException.Message);
                    }
                    //CID 0859 //2012.03.20 //End
                    //CID 1137 //2012.03.17 //End
                }
                else
                {
                    //CID 0859 //2012.03.20 //Begin
                    if (throwsException)
                    {
                        throw new InfoJetException(MessageUtil.Error2Submit() + submitName, null);
                    }
                    else
                    {
                        if (submitException != null)
                        {
                            FormUtil.addModalMessage(form, MessageUtil.Error2Submit() + submitName, submitException, submit.WSAdapter != null);
                        }
                        //CID 0603 //2010.05.31 //Begin
                        else
                        {
                            form.ModalMessageList.Add(MessageUtil.Error2Submit() + submitName);
                        }
                        //CID 0603 //2010.05.31 //End
                    }
                    //CID 0859 //2012.03.20 //End
                }
            }
            //CID 0939 //2011.06.02 //End
        }

        //CID 1137 //2012.03.19 //Begin
        public static void submitByRuleAction(Form form, RuleAction ruleAction)
        {
            form.OperationFailed = false;
            //CID 0859 //2012.03.19 //Begin
            int errorCount = FormIsValidateWorker.Validate(form);
            if (errorCount > 0)
            {
                if (form.ModalMessageList == null)
                {
                    form.ModalMessageList = new ArrayList();
                }
                form.ModalMessageList.Add(MessageUtil.FormCannotBeSubmittedForValidationErrors());
            }
            //CID 0859 //2012.03.19 //End
            string submitName = "";
            try
            {
                if (ruleAction.SubmitDataAdapter is DataWSAdapter)
                {
                    DataWSAdapter wsAdapter = (DataWSAdapter)(DataWSAdapter)ruleAction.SubmitDataAdapter;
                    submitName = wsAdapter.ServiceUrl;
                    DataObjectWSSubmitWorker.submitDoc(form, wsAdapter);
                }
                else if (ruleAction.SubmitDataAdapter is DataDavAdapter)
                {
                    DataDavAdapter davAdapter = (DataDavAdapter)ruleAction.SubmitDataAdapter;
                    submitName = davAdapter.Name;
                    DataObjectDavSubmitWorker.submitDoc(form, davAdapter);
                }
                else if (ruleAction.SubmitDataAdapter is DataEmailAdapter)
                {
                    DataEmailAdapter emailAdapter = (DataEmailAdapter)ruleAction.SubmitDataAdapter;
                    submitName = emailAdapter.Name;
                    DataObjectEmailSubmitWorker.submitDoc(form, emailAdapter);
                }
                //CID 1134 //2012.03.14 //Begin
                else if (ruleAction.SubmitDataAdapter is SubmitToHostAdapter)
                {
                    SubmitToHostAdapter toHostAdapter = (SubmitToHostAdapter)ruleAction.SubmitDataAdapter;
                    submitName = toHostAdapter.Name;
                    SubmitToHostWorker.submitDoc(form, toHostAdapter);
                }
                //CID 1134 //2012.03.14 //End
            }
            catch (Exception e)
            {
                FormUtil.addModalMessage(form, MessageUtil.Error2Submit() + submitName, e, ruleAction.SubmitDataAdapter is DataWSAdapter);
                //CID 1137 //2012.03.17 //Begin
                form.OperationFailed = true;
                //CID 1137 //2012.03.17 //End
            }
        }
        //CID 1137 //2012.03.19 //End
    }
}
